<script setup lang="ts">
import { Handle, Position } from '@vue-flow/core';
import { NIcon, NPopconfirm } from 'naive-ui';
import { Close, Edit, TaskComplete } from '@vicons/carbon';

interface NodeData {
  label: string;
  description?: string;
}

const props = defineProps<{
  id: string;
  data: NodeData;
}>();

const emit = defineEmits<{
  deleteNode: [nodeId: string];
}>();

// 确认删除
function handleConfirmDelete() {
  emit('deleteNode', props.id);
}
</script>

<template>
  <div class="approval-node">
    <!-- 顶部输入连接点 -->
    <Handle type="target" :position="Position.Top" class="handle" />

    <!-- 删除按钮 -->
    <NPopconfirm
      placement="top"
      negative-text="取消"
      positive-text="确定"
      @positive-click="handleConfirmDelete"
    >
      <template #trigger>
        <button class="delete-btn" title="删除节点">
          <NIcon :size="12">
            <Close />
          </NIcon>
        </button>
      </template>
      确定要删除此节点吗？
    </NPopconfirm>

    <!-- 顶部标题栏 -->
    <div class="node-header">
      <div class="node-title">
        {{ data.label }}
      </div>
      <div class="edit-icon">
        <NIcon :size="14">
          <Edit />
        </NIcon>
      </div>
      <div class="icon-wrapper">
        <NIcon :size="22">
          <TaskComplete />
        </NIcon>
      </div>
    </div>

    <!-- 节点内容 -->
    <div class="node-body">
      <div class="node-description">
        {{ data.description || '未设置审批对象' }}
      </div>
    </div>

    <!-- 底部输出连接点 -->
    <Handle type="source" :position="Position.Bottom" class="handle" />
  </div>
</template>

<style scoped>
.approval-node {
  border-radius: 8px;
  background: white;
  min-width: 240px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
  transition: all 0.2s;
  overflow: visible;
  position: relative;
}

.approval-node:hover {
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.12);
}

.delete-btn {
  position: absolute;
  top: -8px;
  right: -8px;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 20px;
  height: 20px;
  border: none;
  background: #ef4444;
  border-radius: 50%;
  cursor: pointer;
  color: white;
  padding: 0;
  z-index: 10;
  opacity: 0;
  transition: all 0.2s;
}

.approval-node:hover .delete-btn {
  opacity: 1;
}

.delete-btn:hover {
  background: #dc2626;
  transform: scale(1.15);
}

.node-header {
  background: #fb923c;
  padding: 12px 16px;
  display: flex;
  align-items: center;
  gap: 12px;
  color: white;
  border-radius: 8px 8px 0 0;
}

.node-title {
  flex: 1;
  font-size: 15px;
  font-weight: 600;
}

.edit-icon,
.icon-wrapper {
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  opacity: 0.9;
  transition: opacity 0.2s;
}

.edit-icon:hover {
  opacity: 1;
}

.icon-wrapper {
  background: rgba(0, 0, 0, 0.15);
  border-radius: 6px;
  padding: 6px;
  opacity: 1;
}

.node-body {
  padding: 16px;
  background: white;
}

.node-description {
  font-size: 14px;
  color: #333;
  line-height: 1.5;
}

.handle {
  width: 8px;
  height: 8px;
  background: #3b82f6;
  border: 2px solid white;
  border-radius: 50%;
}
</style>
